<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . '/includes/pdoorm.inc.php';

//header('Content-Type: text/plain');

if (isset($_COOKIE[OPERATOR])) { 
 $coperator = json_decode($_COOKIE[OPERATOR]);
}

//echo str_replace("world","<span style=\"background-color:#FF0\">John</span>","Hello world!");

try {
  $options = MiscUtils::getParam('opts', json_encode(array()));
  $options = json_decode(urldecode($options));
  
  $u = MiscUtils::getParam('u', json_encode(array()));
  $u = json_decode(urldecode($u));
  $user=isset($u->user)? $u->user : '';
  $pass=isset($u->pass)? $u->pass : '';  
  
  //$options = new stdClass();
  //$options->name = array('Rochelle Thrupp');
  //$options->venue = array('13901 Central Avenue','Angel oresanz','Angelinas Restaurant','Unknow');
  //$options->contact = array('5227289');
  //$options->wd = array('2009-02-14','2010-03-19');
  
   $result = new stdClass();
  // $tmp = new stdClass();
   $result->data = array();
   $tmpaf = array();

  
  
    if(!empty($options->name)  || !empty($options->contact) || !empty($options->wd) || !empty($options->venue))
    {
	  $sql=" SELECT DISTINCT c.e_oid as id,c.bridename as bride_name,c.bridephone as bride_phone,c.groomname as groom_name,c.groomphone as groom_phone,c.ceremony as cer,c.ceremony2 as cer2,c.reception as rec,c.reception2 as rec2,c.bridemobile as bride_mobile,c.groommobile as groom_mobile,
	                  c.brideemail as bride_email,c.groomemail as groom_email,c.brideaddrLn1 as bride_addr1,c.brideaddrLn2 as bride_addr2,c.groomaddrLn1 as groom_addr1,c.groomaddrLn2 as groom_addr2 FROM ns_v1myorder AS m LEFT JOIN ns_v1customer AS c ON m.customerId=c.e_oid
	         WHERE 1=1  AND ( ";
	  
	  $namestr = '';
	  $contractstr = '';
	  $wdstr = '';
	  $venstr = '';
	  $subsql = '';    	
    	
      if(!empty($options->name))
      {
      	$namearr =array();
        foreach($options->name as $oname)
        {
            $narr = split (' ', $oname);
            
            foreach ($narr as $na)
            {
            	$val = trim($na);
            	if( !empty( $val ) )
            	{
            	   $namearr[] = $na;
            	}
            }
            
        }

      	
      	foreach($namearr as $oname)
      	{
      		$namestr .= "  CONCAT(c.bridename,'||',c.groomname) LIKE '%".$oname."%'   OR";
      	}
      	
      	$namestr = substr($namestr,0,-2);
      	
      	$subsql .="  ( $namestr )  ";
      }

      if(!empty($options->contact))
      {
        
        foreach($options->contact as $ocon)
        {
            $contractstr .= "  CONCAT(c.bridemobile,'||',c.bridephone,'||',c.groommobile,'||',c.groomphone)  LIKE '%".$ocon."%'   OR";
        }
        
        $contractstr = substr($contractstr,0,-2);
        
	       if(empty($subsql) )
	       {
	       	$subsql .="  ( $contractstr )  ";
	       }else{ 
	        $subsql .=" OR ( $contractstr )  ";
	       }
      }

      if(!empty($options->wd))
      {
        
        foreach($options->wd as $owd)
        {
            $wdstr .= "  LEFT(FROM_UNIXTIME(m.date),10)= '".$owd."'  OR";
        }
        
        $wdstr = substr($wdstr,0,-2);
       
        if(empty($subsql))
        {
          $subsql .="  ( $wdstr )  ";
        }else{
          $subsql .=" OR ( $wdstr )  ";
        }
        
      }      
      

      if(!empty($options->venue))
      {
        
        foreach($options->venue as $oven)
        {
            $venstr .= "  CONCAT(c.ceremony,'||',c.ceremony2,'||',c.reception,'||',c.reception2)   LIKE '%".$oven."%'  OR";
        }
        
        $venstr = substr($venstr,0,-2);
        
        if(empty($subsql))
        {
          $subsql .="  ( $venstr )  ";
        }else{
          $subsql .=" OR ( $venstr )  ";
        }
      }  
      
      if(!empty($subsql))
      {
      	$sql .="  $subsql ";
      }else{
      	$sql .="  1=1 ";
      	
      }
      
      $sql .=" )"; 
      
      //echo $sql;
      //echo $namestr;
  $tmp=getall($sql,1);
  $count = count_num($sql);
   }else{
   	$tmp='';
   	$count = 0;
   }
  

  
  /*$strtable .="<tr style=\"font-weight:bold;background-color:#dcaaaa;height:30px;\">";
  $strtable .="<td width='150px;'>Customer No</td>";
  $strtable .="<td width='16%'>Bride</td>";
  $strtable .="<td width='16%'>Phone</td>";
  $strtable .="<td width='16%'>Groom</td>";
  $strtable .="<td width='16%'>Phone</td>";
  $strtable .="</tr>";*/

  
  
  if(!empty($tmp))
  {
  	//$strtable ="<table width='972px' style=\"margin:-8px 0 10px -7px; border-left:solid 1px #ddd;border-top:solid 1px #ddd;\"; cellspacing='0';cellpadding='0'>";
    foreach($tmp as $t)
    {
    	$sqlcus ="SELECT  * FROM ns_v1customer where e_oid='".$t['id']."'";
    	$tmpcus=getone($sqlcus,1);    	
    	
    	$sqlorder ="SELECT  LEFT(FROM_UNIXTIME(date),10) AS mdate FROM ns_v1myorder where customerid='".$t['id']."' ORDER BY createdDate ASC";
    	$tmporder=getall($sqlorder,1);

    	
    	$i=0;
    	if(!empty($options->name))
        {  
	        $namearr1 =array();
	        foreach($options->name as $oname)
	        {
	            $narr1 = split (' ', $oname);
	            
	            foreach ($narr1 as $na1)
	            {
	                $val1 = trim($na1);
	                if( !empty( $val1 ) )
	                {
	                   $namearr1[] = $na1;
	                }
	            }
	            
	        }  

	        
	        $bridearr = strtoarray($t['bride_name']);
	        $grooomarr = strtoarray($t['groom_name']);
        	
	        $bridechk = (array_intersect(arrayucwords($bridearr),arrayucwords($namearr1)));
	        $grommchk = (array_intersect(arrayucwords($grooomarr),arrayucwords($namearr1)));
        	
	    	if(!empty($bridechk)  || !empty($grommchk))
	    	{
	    		$i +=1;
	    	}
        }
        
        if(!empty($options->contact))
        {
            if(in_array($t['bride_phone'],$options->contact)  || in_array($t['groom_phone'],$options->contact) || in_array($t['bride_mobile'],$options->contact)  || in_array($t['groom_mobile'],$options->contact))
            {
                $i +=1;
            }
        }
        
        if(!empty($options->wd))
        {

            if(!empty($tmporder))
            {
	            foreach($tmporder as $order)
	            {
	                if(in_array($order['mdate'],$options->wd) )
		            {
		                $i +=1;
		                break;
		            }	                
	            }
            }        	
        	
        	

        }        

        if(!empty($options->venue))
        {
            if(in_array(arrayucwords($t['cer']),arrayucwords($options->venue))  || in_array(arrayucwords($t['cer2']),arrayucwords($options->venue)) || in_array(arrayucwords($t['rec']),arrayucwords($options->venue))  || in_array(arrayucwords($t['rec2']),arrayucwords($options->venue)))
            {
                $i +=1;
            }
        }

        $istr = $i == 0 ? '':$i;
        if($i == 0)
        {	
        	continue;
        }
        
        
        $t['num']=$istr;
        $wdate = is_array($tmporder) ? $tmporder[0]['mdate']: '';
        
        $t['date_wedding'] = $wdate;
        $tm1 = array();
        $tm2 = array();
        
        if(!empty($t['cer'])) array_push($tm1,$t['cer']);
        if(!empty($t['cer2'])) array_push($tm1,$t['cer2']); 
        
        if(!empty($t['rec'])) array_push($tm2,$t['rec']);
        if(!empty($t['rec2'])) array_push($tm2,$t['rec2']);
        
        $t['ceremonys'] =$tm1;
        $t['receptions'] =$tm2;
        
        $tmpaf[]=$t;
		 
    } 

    
  }
  
  echo json_encode($tmpaf);

} catch (PDOException $e) {
  echo $e->getMessage();
}

function arrayucwords($str)
{
	if(is_array($str))
	{
		$tmp = array();
		foreach($str as $onearr)
		{
			$tmp[] = ucwords($onearr);
		}
		return $tmp;
	}else{
		return ucwords($str);
	}
	
}


function strtoarray($str)
{
	$namearr1 = array();
               $narr1 = split (' ', $str);
                
                foreach ($narr1 as $na1)
                {
                    $val1 = trim($na1);
                    if( !empty( $val1 ) )
                    {
                       $namearr1[] = $na1;
                    }
                }

                return $namearr1;
}

/**
 * $str 需要高亮显示的字符串,
 * $keyword 关键字  可以为数组,
 * $issplit=''  是否把当个关键字按照空格拆开成多个关键字
 */
function keywordlight($str,$keyword,$issplit='')
{
	if(is_array($keyword))
	{
		foreach($keyword as $onekw)
		{
			if(!empty($issplit))
			{
				$splitarr = strtoarray($onekw);
				 
				foreach($splitarr as $onesp)
				{
					$str = str_replace($onesp,"<span style=\"background-color:#FF0\">".$onesp."</span>",$str);
				}
				
			}else{
		        $str = str_replace($onekw,"<span style=\"background-color:#FF0\">".$onekw."</span>",$str);
			}
		}
	}else{
		$str = str_replace($keyword,"<span style=\"background-color:#FF0\">".$keyword."</span>",$str);
	}
	
	return $str;
}

?>